<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="合作门店昵称" prop="shopName">
        <el-input
          v-model="queryParams.shopName"
          placeholder="请输入合作门店昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="门店类型" prop="shopTypeId">
        <el-select
          v-model="queryParams.shopTypeId"
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="item in formss"
            :label="item.typeName"
            :value="item.id"
            :key="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="合作店所属" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入合作店所属"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="省" prop="shopProvince">
        <el-input
          v-model="queryParams.shopProvince"
          placeholder="请输入省"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="市" prop="shopCity">
        <el-input
          v-model="queryParams.shopCity"
          placeholder="请输入市"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区/县" prop="shopCounty">
        <el-input
          v-model="queryParams.shopCounty"
          placeholder="请输入区/县"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="shopPerson">
        <el-input
          v-model="queryParams.shopPerson"
          placeholder="请输入联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人电话" prop="shopPhone">
        <el-input
          v-model="queryParams.shopPhone"
          placeholder="请输入联系人电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['shop:add']"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table
      v-loading="loading"
      :data="shopList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="合作门店昵称" align="center" prop="nickName" />
      <el-table-column label="合作店所属" align="center" prop="userName" />
      <el-table-column label="门店类型" align="center" prop="shopTypeName" />
      <el-table-column label="门店名称" align="center" prop="shopName" />
      <el-table-column label="省" align="center" prop="shopProvince" />
      <el-table-column label="市" align="center" prop="shopCity" />
      <el-table-column label="区/县" align="center" prop="shopCounty" />
      <el-table-column
        label="详细地址"
        align="center"
        prop="shopAddress"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="经度" align="center" prop="shopLongitude" />
      <el-table-column label="纬度" align="center" prop="shopLatitude" />
      <el-table-column label="联系人" align="center" prop="shopPerson" />
      <el-table-column label="联系人电话" align="center" prop="shopPhone" />
      <el-table-column label="门店门头照片" align="center" prop="shopPicture">
        <template slot-scope="scope">
          <el-image
            v-if="scope.row.shopPicture"
            class="img"
            :src="scope.row.shopPicture"
            :preview-src-list="[scope.row.shopPicture]"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['shop:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['shop:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="门店账号">
          <el-select v-model="form.userId" placeholder="请选择门店账号">
            <el-option
              v-for="item in forms"
              :label="item.nickName"
              :value="item.userId"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="门店类型">
          <el-select v-model="form.shopTypeId" placeholder="请选择门店类型">
            <el-option
              v-for="item in formss"
              :label="item.typeName"
              :value="item.id"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="门店名字" prop="shopName">
          <el-input v-model="form.shopName" placeholder="请输入门店名字" />
        </el-form-item>
        <el-form-item label="所在的城市">
          <el-cascader
            :options="regionData"
            @change="handleChange"
            class="full-width"
            size="large"
            v-model="form.shopPccCode"
            placeholder="请选择您所在的城市"
          />
        </el-form-item>

        <!-- <el-form-item label="省" prop="shopProvince">
          <el-input v-model="form.shopProvince" placeholder="请输入省" />
        </el-form-item>
        <el-form-item label="市" prop="shopCity">
          <el-input v-model="form.shopCity" placeholder="请输入市" />
        </el-form-item>
        <el-form-item label="区/县" prop="shopCounty">
          <el-input v-model="form.shopCounty" placeholder="请输入区/县" />
        </el-form-item> -->

        <el-form-item label="获取门店经纬度" prop="shopLongitude">
          <el-button @click="openDialogs">获取门店经纬度</el-button>
        </el-form-item>
        <el-form-item label="详细地址" prop="shopAddress">
          <el-input v-model="form.shopAddress" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="经度" prop="shopLongitude">
          <el-input v-model="form.shopLongitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="shopLatitude">
          <el-input v-model="form.shopLatitude" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="联系人" prop="shopPerson">
          <el-input v-model="form.shopPerson" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系人电话" prop="shopPhone">
          <el-input
            maxlength="11"
            v-model="form.shopPhone"
            placeholder="请输入联系人电话"
          />
        </el-form-item>
        <el-form-item label="门店门头" prop="shopPicture">
          <upimg
            :up-with="240"
            :imgSrc="form.shopPicture"
            :up-height="160"
            :up-bradius="10"
            uptext="请上传门店门头图片"
            @changeName="imgUrlfun3"
          ></upimg>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 地图选点 -->
    <mapSelect @chooseOrgAddr="chooseOrgAddr" ref="stn"></mapSelect>
  </div>
</template>

<script>
import {
  listShop,
  getShop,
  delShop,
  addShop,
  updateShop,
  getrole,
  listRole,
} from "@/api/system/mendian";

import mapSelect from "../../../components/MapMendian";
import Upimg from "../../../components/UploadImg";
import { regionData, codeToText } from "element-china-area-data";
export default {
  name: "Shop",
  components: {
    mapSelect,
    Upimg,
  },
  data() {
    return {
      indexs: "",
      indexss: "",
      isDialogVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      codes: "hezuodian",
      // 表格数据
      shopList: [],
      forms: [],
      formss: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        shopCity:'',
        shopCounty:'',
        shopName:'',
        shopPerson:'',
        shopPhone:'',
        shopProvince:'',
        shopTypeId:'',
        userName:'',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        shopLongitude: [
          { required: true, message: "请选择经纬度", trigger: "blur" },
        ],
        shopLatitude: [
          { required: true, message: "请选择经纬度", trigger: "blur" },
        ],
        shopAddress: [
          { required: true, message: "请输入详细地址", trigger: "blur" },
        ],
      },
      regionData: regionData,
    };
  },
  created() {
    this.getList();
    listRole(this.queryParams).then((response) => {
      this.formss = response.rows;
    });
  },
  methods: {
    handleChange(value) {
      console.log(value);

      this.form.shopProvince = codeToText[value[0]];
      this.form.shopCity = codeToText[value[1]];
      this.form.shopCounty = codeToText[value[2]];
    },
    imgUrlfun3(e) {
      if (e) {
        this.form.shopPicture = e.url;
      } else {
        this.form.shopPicture = "";
      }
    },
    openDialogs() {
      this.$refs.stn.openDialog(); // 打开对话框
    },
    // 选取地理位置后的回调
    chooseOrgAddr(data) {
      console.log(data);
      this.form.shopAddress = data.address;
      this.form.shopLatitude = data.lat;
      this.form.shopLongitude = data.lng;
      this.$refs.stn.closeDialog();
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      listShop(this.queryParams).then((response) => {
        this.shopList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        userName: null,
        nickName: null,
        shopTypeId: null,
        shopTypeName: null,
        shopName: null,
        shopProvince: null,
        shopCity: null,
        shopCounty: null,
        shopAddress: null,
        shopLongitude: null,
        shopLatitude: null,
        shopPerson: null,
        shopPhone: null,
        shopPicture: null,
        createUserId: null,
        createTime: null,
        shopPccCode: [],
      };
      this.resetForm("form");
    },
    openDialog(e) {
      console.log(e);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加";
      getrole(this.codes).then((response) => {
        this.forms = response.data;
      });
      this.queryParams.pageSize = 500;
      listRole(this.queryParams).then((response) => {
        this.formss = response.rows;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      let data = {
        id: row.id,
      };
      getrole(this.codes).then((response) => {
        this.forms = response.data;
      });
      this.queryParams.pageSize = 500;
      listRole(this.queryParams).then((response) => {
        this.formss = response.rows;
      });
      getShop(data).then((response) => {
        this.form = response.data;
        this.form.shopPccCode = row.shopPccCode.split(",");
        this.open = true;
        this.title = "修改";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            if (this.form.userId) {
              this.form.userName = this.forms.filter(
                (unit) => unit.userId == this.form.userId
              )[0].userName;
              this.form.nickName = this.forms.filter(
                (unit) => unit.userId == this.form.userId
              )[0].nickName;
            }
            if (this.form.shopTypeId) {
              this.form.shopTypeName = this.formss.filter(
                (unit) => unit.id == this.form.shopTypeId
              )[0].typeName;
            }
            this.form.shopPccCode = this.form.shopPccCode.join(",");
            updateShop(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.shopPccCode = this.form.shopPccCode.join(",");
            if (this.form.userId) {
              this.form.userName = this.forms.filter(
                (unit) => unit.userId == this.form.userId
              )[0].userName;
              this.form.nickName = this.forms.filter(
                (unit) => unit.userId == this.form.userId
              )[0].nickName;
            }
            if (this.form.shopTypeId) {
              this.form.shopTypeName = this.formss.filter(
                (unit) => unit.id == this.form.shopTypeId
              )[0].typeName;
            }
            addShop(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let data = {
        ids: [row.id],
      };
      this.$modal
        .confirm("是否确认删除此项数据？")
        .then(function () {
          return delShop(data);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
<style>
.img {
  width: 120px;
  height: 100px;
  border-radius: 10px;
}
</style>
